﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GraphicsEngineNamespace
{
    /// <summary>
    /// Klasa zarządzająca teksturami.
    /// </summary>
    public class TextureManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private ContentManager content;

        public ContentManager Content
        {
            get { return content; }
            set { content = value; }
        }

        /// <summary>
        /// Ścieżka do katalogu z teksturami.
        /// </summary>
        private string path = "Texture\\";

        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        /// <summary>
        /// Nazwy tekstur.
        /// </summary>
        private static List<string> name;

        public static List<string> Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Lista zawierająca wszystkie tekstury do wczytania.
        /// </summary>
        private static List<Texture2D> texture;

        public static List<Texture2D> Texture
        {
            get { return texture; }
            set { texture = value; }
        }

        public TextureManager(Game game)
            : base(game)
        {
            Content = new ContentManager(game.Services);
            Content.RootDirectory = "Content";

            Name = new List<string>();
            Texture = new List<Texture2D>();
        }

        /// <summary>
        /// Ustawienie nazw tekstur, które będą wczytywane.
        /// </summary>
        private void SetTextureName()
        {   
            Name.Add("default_ambocc_map");
            Name.Add("default_heightmap");
            Name.Add("default_normal_map");
            Name.Add("default_surface_map");
            Name.Add("grass");
            Name.Add("grassBump");
            Name.Add("heightmap");
            Name.Add("rock");
            Name.Add("rockBump");
            Name.Add("sand");
            Name.Add("sandBump");
            Name.Add("snow");
            Name.Add("snowBump");
        }

        /// <summary>
        /// Załadowanie tekstur.
        /// </summary>
        private void LoadTexture()
        {
            //Texture2D a;
            //a = Texture2D.FromFile(GraphicsDevice, "G:\\Wsiz\\Semestr 6\\Projekt grupowy\\Exploration\\Exploration\\GraphicsEngine\\Content\\Texture\\rock.jpg");
            //a.Save(Content.RootDirectory+"\\"+Path+"to.jpg", ImageFileFormat.Jpg);
            
            Name.ForEach(delegate(string nazwa) { Texture.Add(Content.Load<Texture2D>(Path + nazwa)); });
        }

        /// <summary>
        /// Zwraca teksturę.
        /// </summary>
        /// <param name="name"></param>
        public static Texture2D GetTexture(string name)
        {
            //int i = Name.FindIndex(delegate(string na) { return (na.Equals(name)); }); 
            return Texture[Name.IndexOf(name)];
        }

        /// <summary>
        /// Zwraca teksturę.
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static Texture2D GetTexture(int index)
        {
            return Texture[index];
        }

        public override void Initialize()
        {
            SetTextureName();
            base.Initialize();
        }

        protected override void LoadContent()
        {
            LoadTexture();
        }

        protected override void UnloadContent()
        {
            Content.Unload();
        }
    }
}